package com.a3dweijing.wetouch2.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.AlerDialogHelp;
import com.a3dweijing.wetouch2.Util.GlideUtil;
import com.a3dweijing.wetouch2.Util.ProgressDialogHelp;
import com.a3dweijing.wetouch2.network.RetrofitUtil;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.OrderListBean;
import com.a3dweijing.wetouch2.bean.ResultMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/4/4.
 */

public class RefundActivity extends BaseActivity
{
    public static int REFUNDACTIVITYCODE = 44;
    
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    
    @BindView(R.id.imageview)
    ImageView imageview;
    
    @BindView(R.id.name)
    TextView name;
    
    @BindView(R.id.yuanying)
    TextView yuanying;
    
    @BindView(R.id.money)
    TextView money;
    
    @BindView(R.id.kuaidi)
    TextView kuaidi;
    
    @BindView(R.id.dingdian_number)
    TextView dingdianNumber;
    
    @BindView(R.id.sbmit)
    TextView sbmit;
    
    private OrderListBean item;
    
    private ProgressDialog dialog;
    
    private AlertDialog alertDialog;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.refund_activity);
        ButterKnife.bind(this);
        item = (OrderListBean)getIntent().getSerializableExtra("item");
        if (item == null)
            return;
        initView();
    }
    
    private void initView()
    {
        topText.setText("申请退款");
        final String[] strings = new String[] {"质量问题", "买错了", "买多了", "其他"};
        dialog = ProgressDialogHelp.create(this, "提示", "提交中...", false);
        alertDialog = AlerDialogHelp.createForList(this, "请选择一个原因", strings, false, new CallBack()
        {
            @Override
            public void doNext(int what, Object obj)
            {
                yuanying.setText("退款原因:" + strings[what]);
            }
        });
        GlideUtil.getInstance().showImage(this, item.getGoods_imgurl(), imageview);
        name.setText(item.getGoods_name());
        money.setText("退款金额：" + item.getOrder_total());
        dingdianNumber.setText("订单号：" + item.getExpress_code());
    }
    
    public static void startActivity(Activity activity, OrderListBean item)
    {
        Intent intent = new Intent(activity, RefundActivity.class);
        intent.putExtra("item", item);
        activity.startActivityForResult(intent, REFUNDACTIVITYCODE);
    }
    
    @OnClick({R.id.back, R.id.sbmit, R.id.yuanying})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.back:
                finish();
                break;
            case R.id.sbmit:
                sbmit();
                break;
            case R.id.yuanying:
                alertDialog.show();
                break;
        }
    }
    
    private void sbmit()
    {
        dialog.show();
        Observable<ResultMsg> observable = RetrofitUtil.getInstance().getApiOfPhp().refund(
            Integer.parseInt(Singleton.getInstance().getUser().getUser_id()),
            item.getOrder_id(),
            yuanying.getText().toString());
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
            new Observer<ResultMsg>()
            {
                @Override
                public void onSubscribe(Disposable d)
                {
                    disposables.add(d);
                }
                
                @Override
                public void onNext(ResultMsg resultMsg)
                {
                    if (resultMsg != null && resultMsg.isFlag())
                    {
                        setResult(RESULT_OK);
                        finish();
                    }
                    Toast.makeText(RefundActivity.this, resultMsg.getMessage(), Toast.LENGTH_SHORT).show();
                }
                
                @Override
                public void onError(Throwable e)
                {
                    dialog.dismiss();
                    Toast.makeText(RefundActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                }
                
                @Override
                public void onComplete()
                {
                    dialog.dismiss();
                }
            });
    }
}
